package com.campus_assistant.controller;

import com.campus_assistant.common.ToVue;
import com.campus_assistant.config.db.DynamicDataSource;
import com.campus_assistant.pojo.db1.Courses;
import com.campus_assistant.pojo.db1.Scores;
import com.campus_assistant.pojo.vo.StudentScores;
import com.campus_assistant.service.db1.CoursesService;
import com.campus_assistant.service.db1.StudentScoresService;
import com.campus_assistant.service.db1.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentScoresController {
    @Autowired
    private StudentScoresService studentScoresService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CoursesService coursesService;

    @RequestMapping("/selectStudentScoresAll")//查询所有学生成绩
    public ToVue selectStudentScoresAll(){
        DynamicDataSource.dbName.set("db1");
        List<StudentScores> studentScores = studentScoresService.selectStudentScoresAll();
        return ToVue.suc(studentScores);

    }
    @RequestMapping("/selectStudentScoreByStudentName")//查询某位学生成绩
    public ToVue selectStudentScoreByStudentName(@RequestParam("studentName") String studentName){
        DynamicDataSource.dbName.set("db1");
        System.out.println("传入的参数为："+studentName);
        List<StudentScores> studentScores = studentScoresService.selectStudentScoreByStudentName(studentName);
        return ToVue.suc(studentScores);

    }
    @RequestMapping("/selectStudentScoreByCourseName")//查询某门课程的所有成绩
    public ToVue selectStudentScoreByCourseName(@RequestParam("courseName") String courseName){
        DynamicDataSource.dbName.set("db1");
        System.out.println("学科为："+courseName);
        List<StudentScores> studentScores = studentScoresService.selectStudentScoreByCourseName(courseName);
        System.out.println(studentScores);
        return ToVue.suc(studentScores);

    }
    @RequestMapping("/selectStudentScoreByTeacherName")//查询某老师任教课程的所有成绩
    public ToVue selectStudentScoreByTeacherName(@RequestParam("teacherName") String teacherName){
        DynamicDataSource.dbName.set("db1");
        System.out.println(teacherName);
        List<StudentScores> studentScores = studentScoresService.selectStudentScoreByTeacherName(teacherName);
        System.out.println(studentScores);
        return ToVue.suc(studentScores);
    }
    @RequestMapping("/addStudentScore")
    public ToVue addStudentScore(@RequestParam("result")String message){
        DynamicDataSource.dbName.set("db1");
        System.out.println("参数为："+message);
        String[] messages = message.split(" ");//根据空格分隔信息
        String sName=messages[0];
        String sNum=messages[1];
        String courses=messages[2];
        String teacherName=messages[3];
        float score=Float.parseFloat(messages[4]);
        int sId=studentService.selectStudentId(sNum);
        int cId=coursesService.getIdByCourseNameAndTeacherName(courses,teacherName);
        StudentScores studentScores=new StudentScores(sId,cId,sName,sNum,courses,score);
        return ToVue.suc(studentScores);
    }
}
